<template>
  <div class="pwd-login">
    <div>
      <Header>登录</Header>
      <div class="login-main">
        <div class="login-tel">
          <input type="text" v-model="userTel" placeholder="请输入手机号" />
        </div>
        <div class="login-pwd">
          <input type="password" v-model="userPwd" placeholder="请输入密码" />
        </div>
        <div class="login-btn" @click="login">登录</div>
        <div class="login-more">
          <div @click="goBack">短信登录</div>
          <div @click="findBackPwd">找回密码</div>
          <div @click="goRegister">快速注册</div>
        </div>
      </div>
    </div>
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import Header from "@/views/login/components/Header.vue";
import { Toast } from "mint-ui";
import { mapMutations } from "vuex";

export default {
  name: "PwdLogin",
  data() {
    return {
      userTel: "",
      userPwd: "",
      rules: {
        userTel: {
          rule: /^1[3456789]\d{9}$/,
          msg: "手机号不能为空，并且为11位数字",
        },
        userPwd: {
          rule: /^\w{6,12}$/,
          msg: "密码不能为空，并且为6-12位",
        },
      },
    };
  },
  components: {
    Header,
  },
  methods: {
    ...mapMutations('user', ['userLogin']),
    async login() {
      //前端验证
      if (!this.validate("userTel")) return;
      if (!this.validate("userPwd")) return;

      //后端验证
      let res = await this.$http.login({
        userTel: this.userTel,
        userPwd: this.userPwd,
      });
      if (res.code == 200) {
        Toast("登录成功");
        //存储用户信息
        this.userLogin(res.data);
        //跳转from页面
        this.$router.replace("/my");
      } else {
        Toast(res.data);
      }
    },
    validate(key) {
      let flag = true;
      if (!this.rules[key].rule.test(this[key])) {
        flag = false;
        Toast(this.rules[key].msg);
      }
      return flag;
    },
    goBack() {
      this.$router.replace("/login");
    },
    findBackPwd() {
      this.$router.replace("/recovery");
    },
    goRegister() {
      this.$router.replace("/register");
    },
  },
};
</script>

<style lang="scss" scoped>
.pwd-login {
  .login-main {
    padding: 0.5333rem;

    .login-tel,
    .login-pwd {
      width: 100%;
      height: 1.1733rem;

      input {
        outline: none;
        border: none;
        height: 100%;
        width: 100%;
        font-size: 0.4267rem;
        padding-left: 0.2667rem;
      }
    }

    .login-btn {
      background-color: #b0352f;
      width: 100%;
      height: 1.1733rem;
      color: #fff;
      text-align: center;
      line-height: 1.1733rem;
      font-size: 0.4267rem;
      border-radius: 0.08rem;
    }
    .login-more {
      font-size: 0.32rem;
      display: flex;
      justify-content: space-between;
    }
  }
  .login-main > div {
    margin-bottom: 0.5333rem;
  }
}
</style>